package com.qf.threadpool;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPoolTest2_2 {
	static boolean numFlag = false;
	static boolean charFlag = false;
	static boolean flag = true;
	static int i =1;
	static char c ='A';
	public static void main(String[] args) {
		/*
		 * 
		 * 作业：
		创建两个线程，第一个线程输出数值  1   2   3...第二个线程输出字母  A  B  C..
		要求输出的顺序： 12A34B56C......5152Z   规律：每输出两个数值后输出一个字母
		 * 
		 * */
		Lock lock = new ReentrantLock();
		Condition withNumCondition = lock.newCondition();
		Condition withWordCondition = lock.newCondition();
		
		Thread t1 =new Thread(new Runnable() {
			
			@Override
			public void run() {
		
			for(int j=0;j<26;j++) {
				try {
					//System.out.println("数字锁开始解锁...");
					lock.lock();
					System.out.printf("%d%d",i++,i++);
					withWordCondition.signal();
					if(j!=25)
					withNumCondition.await();
					Thread.sleep(10);
					lock.unlock();
					//System.out.println("数字锁已经被锁住...");
					
				} catch (InterruptedException e) {
			
					e.printStackTrace();
				}
				finally {
					if(j==26) {
						
						Thread.currentThread().stop();
					}
					
				}
				
			}
			
			//Thread.currentThread().stop();
			//Thread.currentThread().interrupt();
			}
			
		});
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				for(int j=0;j<26;j++) {
					try {
						//System.out.println("字母锁开始解锁...");
						lock.lock();
						
						System.out.printf("%c",c++);
						withNumCondition.signal();
						if(j!=25)
						withWordCondition.await();
						Thread.sleep(10);
						lock.unlock();
						//System.out.println("字母锁已经被锁住...");
						
						
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finally {
						if(j==26) {
							
							Thread.currentThread().stop();
						}
						
					}
					
				}
				
				//Thread.currentThread().stop();
				//Thread.currentThread().interrupt();
			}
		});
		System.out.println("t1开始运行...");
		t1.start();
		System.out.println("t2开始运行...");
		t2.start();
	

			//withNumCondition.signalAll();
		
		//System.out.println(withNumCondition);
		//Thread.currentThread().stop();
		
	}
}
